<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <script>
        // 突然有一天，小美和码路同学生气了，码路为了哄小红开心，想送小美礼物，
        // 但是此时小美由于还在生码路的气，所以不会搭理码路。
        // 此时码路想到了小美的闺蜜，让她做代理，然后把礼物送给小美的闺蜜，让闺蜜适时送给小美

        function Flower(name) {
            this.name = name
        }
        var malu = (function () {
            var flower = new Flower('玫瑰花');
            return {
                name: '码路',
                sendFlower: function (target) {
                    target.getFlower(flower, this)
                }
            }
        })();

        // guimi就是一个代理
        var guimi = (function () {
            return {
                name: "闺蜜",
                flower:null,
                sendPeron:null,
                getFlower(flower, person) {
                    this.flower = flower.name;
                    this.sendPeron = person.name;
                    console.log("闺蜜收到了" + person.name + "的" + flower.name);
                },
                getState(state){
                    if(state === "心情很好" && this.flower){
                        xiaomei.getFlower(this.flower,this.sendPeron);
                    }
                }
            }
        })()

        var xiaomei = (function () {
            var state;
            var timer = setInterval(function () {
                if (Math.random() > 0.5) {
                    state = '心情很好'
                } else {
                    state = '心情不好'
                }
                guimi.getState(state)
            }, 1000)
            // guimi.getState(state)
            return {
                getFlower: function (flower, person) {
                    // 接收自闺蜜转接的礼物
                    console.log("小美收到了来自闺蜜转接自" + person + "的" + flower)
                }
            }
        })();
        malu.sendFlower(guimi)
    </script>
</body>

</html>